package com.eeepay.eeepay_v2.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.fragment.FragmentManageCenter;
import com.eeepay.eeepay_v2.fragment.FragmentSaleCenter;
import com.eeepay.eeepay_v2.fragment.InformationFragment;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.SoundPlayUtils;
import com.eeepay.v2_library.dialog.CustomDialog;
import com.eeepay.v2_library.dialog.DialogUtil;
import com.eeepay.v2_library.ui.ABBaseActivity;

public class MainActivity extends ABBaseActivity implements View.OnClickListener {

    private FrameLayout layout_fragment;
    private TextView tab_sale, tab_manage,tab_information;

    private FragmentManager manager;
    private Fragment lastFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initWidget();
        eventOnClick();
        manager = getSupportFragmentManager();

        Fragment fragment = new FragmentSaleCenter();
        //加载第一个tab首页(默认第一个被选中)
        manager.beginTransaction().add(R.id.layout_fragment, fragment, "sale_fragment").commit();
        tab_sale.setEnabled(false);
        tab_manage.setEnabled(true);
        tab_information.setEnabled(true);
        lastFragment = fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initWidget() {
        layout_fragment = getViewById(R.id.layout_fragment);
        tab_sale = getViewById(R.id.tab_sale);
        tab_manage = getViewById(R.id.tab_manage);
        tab_information = getViewById(R.id.tab_information);
    }

    @Override
    protected void eventOnClick() {
        tab_manage.setOnClickListener(this);
        tab_sale.setOnClickListener(this);
        tab_information.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        manager.beginTransaction().hide(lastFragment).commit();
        switch (v.getId()){
            case R.id.tab_sale://销售中心
                SoundPlayUtils.getInstance().play();//播放声音
                Fragment saleFg = manager.findFragmentByTag("sale_fragment");
                manager.beginTransaction().show(saleFg).commit();
                tab_sale.setEnabled(false);
                tab_manage.setEnabled(true);
                tab_information.setEnabled(true);
                lastFragment = saleFg;
                tab_sale.setTextColor(getResources().getColor(R.color.unify_bg));
                tab_information.setTextColor(getResources().getColor(R.color.unify_txt_color_gray));
                tab_manage.setTextColor(getResources().getColor(R.color.unify_txt_color_gray));
                break;
            case R.id.tab_information://add by zhuangzeqin 2017年9月26日17:52:45 更加产品新增资讯模块
                SoundPlayUtils.getInstance().play();//播放声音
                Fragment informationFg = manager.findFragmentByTag("information_fragment");
                if (informationFg==null)
                {
                    informationFg = InformationFragment.getInstance();
                    manager.beginTransaction().add(R.id.layout_fragment, informationFg, "information_fragment").commit();
                }
                else
                {
                    manager.beginTransaction().show(informationFg).commit();
                }
                tab_sale.setEnabled(true);
                tab_manage.setEnabled(true);
                tab_information.setEnabled(false);
                lastFragment = informationFg;
                tab_sale.setTextColor(getResources().getColor(R.color.unify_txt_color_gray));
                tab_manage.setTextColor(getResources().getColor(R.color.unify_txt_color_gray));
                tab_information.setTextColor(getResources().getColor(R.color.unify_bg));
                break;
            case R.id.tab_manage://管理中心
                SoundPlayUtils.getInstance().play();//播放声音
                Fragment manageFg = manager.findFragmentByTag("manage_fragment");
                if(manageFg == null){
                    manageFg = new FragmentManageCenter();
                    manager.beginTransaction().add(R.id.layout_fragment, manageFg, "manage_fragment").commit();
                }else{
                    manager.beginTransaction().show(manageFg).commit();
                }
                tab_sale.setEnabled(true);
                tab_information.setEnabled(true);
                tab_manage.setEnabled(false);
                lastFragment = manageFg;
                tab_sale.setTextColor(getResources().getColor(R.color.unify_txt_color_gray));
                tab_information.setTextColor(getResources().getColor(R.color.unify_txt_color_gray));
                tab_manage.setTextColor(getResources().getColor(R.color.unify_bg));
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

//    public interface NoticeStatusListener{
//        void onNoticeChanged(int noticeCount);
//    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            showExitDialog();
        }
        return super.onKeyDown(keyCode, event);
    }

    private CustomDialog exitDialog;
    private void showExitDialog(){
        if(exitDialog == null){

            exitDialog = DialogUtil.getDoubleCustomDialog(mContext, "温馨提醒", "您确定要退出吗？",
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            UserInfo.removeUserInfo();
                            android.os.Process.killProcess(android.os.Process.myPid());
                            System.exit(0);
                            MainActivity.this.finish();
                        }
                    });
        }
        if(!exitDialog.isShowing()){

            exitDialog.show();
        }
    }
}
